/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.bayu.graph;

import com.bayu.common.lib.CommonUtil;
import com.bayu.controller.MainController;
import com.bayu.services.dao.AvgOdcDao;
import com.bayu.services.dao.OdcDao;
import com.bayu.services.entity.Avgodc;
import com.bayu.services.entity.Odc;
import java.awt.Color;
import java.io.OutputStream;
import java.lang.String;
import java.util.List;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 *
 * @author Bayu
 */
@Controller
public class OdcGraph {

    private OdcDao dao;
    private AvgOdcDao avgOdcDao;
    private String tgl1;
    private String tgl2;

    @Autowired
    public void setDao(OdcDao dao) {
        this.dao = dao;
    }

    @Autowired
    public void setDao(AvgOdcDao avgOdcDao) {
        this.avgOdcDao = avgOdcDao;
    }

    @RequestMapping("/odcgraph.png")
    public void drawChart(OutputStream stream) throws Exception {
        JFreeChart chart = getChart();
        ChartUtilities.writeChartAsPNG(stream, chart,1480,800);
    }

    @RequestMapping("/odcgraph-big.png")
    public void drawChartZoom(OutputStream stream) throws Exception {
        JFreeChart chart = getChart();
        ChartUtilities.writeChartAsPNG(stream, chart, 1880,1000);
    }

    private JFreeChart getChart() {
        DefaultCategoryDataset dataset = getDataset();

        String title = "PLAN & ACTUAL DESPATCH(TON)";
        if(!MainController.startDate.equals("")){
          tgl1 = CommonUtil.formatStringDate(MainController.startDate);
          tgl2 = CommonUtil.formatStringDate(MainController.endDate);
          title = title+"\r\n"+ tgl1+"-"+tgl2;
        }
        final JFreeChart chart = ChartFactory.createLineChart(
            title,
            "Date",
            "Ton",
            dataset,
            PlotOrientation.VERTICAL,
            true,
            true,
            false
        );
        chart.setBackgroundPaint(new Color(204, 255, 204));
        CategoryPlot plot = chart.getCategoryPlot();

        plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
        plot.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);

        CategoryItemRenderer renderer = plot.getRenderer();
        renderer.setSeriesPaint(0, Color.green);
        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setAutoRange(true);
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());


        return chart;
    }

    private DefaultCategoryDataset getDataset() {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        List<Odc> data = this.dao.getAllByDate(MainController.startDate, MainController.endDate);



            String tempDate[] = MainController.startDate.split("-");
            int avgStartMonth = Integer.parseInt(tempDate[1]) + 1;
            int avgEndMonth = Integer.parseInt(tempDate[1]) + 4;

           
            if(avgStartMonth > 12){
                avgStartMonth = avgStartMonth - 12;
            }

            if(avgEndMonth > 12 ){
                avgEndMonth = avgEndMonth - 12;
            }
            String avgStartDate = tempDate[0]+"-"+avgStartMonth+"-"+tempDate[2];
            String avgEndDate = tempDate[0]+"-"+avgEndMonth+"-"+tempDate[2];

        double planTotal ;
        double actTotal;

        for (Odc object : data) {
            String[] date = object.getDate().toString().split("-");
            planTotal = object.getPlan2w()+object.getPlan4w()+object.getPlanexport()+object.getPlanip()+object.getPlanpf();
            actTotal = object.getAct2w()+object.getAct4w()+object.getActexport()+object.getActip()+object.getActpf();

            dataset.addValue(object.getPlan4w(), "Plan 4W", date[2]);
            dataset.addValue(object.getPlan4w(), "Plan 2W", date[2]);
            dataset.addValue(planTotal, "Plan Total", date[2]);
            dataset.addValue(object.getPlanexport(), "Plan Export", date[2]);
            dataset.addValue(object.getPlanip(), "Plan IP", date[2]);
            dataset.addValue(object.getPlanpf(), "Plan P/F", date[2]);
            dataset.addValue(object.getAct2w(), "Act 2W", date[2]);
            dataset.addValue(object.getAct4w(), "Act 4W", date[2]);
            dataset.addValue(object.getActip(), "Act IP", date[2]);
            dataset.addValue(actTotal, "Act Total", date[2]);


        }


        List<Avgodc> data2 = this.avgOdcDao.getAllByDate(avgStartDate,avgEndDate );

        for(Avgodc object : data2){
            String tempDate2[] = object.getDate().toString().split("-");
            String avgMonthName= CommonUtil.getShortMonthName(Integer.parseInt(tempDate2[1])-1);
            dataset.addValue(object.getAvgforce(), "Forc", "Forc "+avgMonthName);

        }

        return dataset;
    }


   /*
    private JFreeChart getChart() {
        XYDataset  dataset = getDataset();

        String title = "PLAN & ACTUAL DESPATCH(TON)";
        if(!MainController.startDate.equals("")){
          tgl1 = CommonUtil.formatStringDate(MainController.startDate);
          tgl2 = CommonUtil.formatStringDate(MainController.endDate);
          title = title+"\r\n"+ tgl1+"-"+tgl2;
        }
        final JFreeChart chart = ChartFactory.createXYLineChart(
            title,
            "Date",
            "Ton",
            dataset,
            PlotOrientation.VERTICAL,
            true,
            true,
            false
        );

        
        chart.setBackgroundPaint(new Color(204, 255, 204));
        XYPlot plot = chart.getXYPlot();
        
        plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
        plot.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);

        DefaultCategoryDataset dataset2 = forcDateSet();


       

        //plot.setDataset(1,dataset2);
        CategoryItemRenderer renderer2 = new LineAndShapeRenderer();

        renderer2.setSeriesPaint(0, Color.BLACK);
        renderer2.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        renderer2.setBaseItemLabelsVisible(true);
        //plot.setRenderer(1, renderer2);



        //CategoryItemRenderer renderer = plot.getRenderer();
        
        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setAutoRange(true);
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());


        return chart;
    }

    private XYDataset  getDataset() {
        XYSeriesCollection dataset = new XYSeriesCollection();

        final XYSeries series1 = new XYSeries("Plan 4W");
        final XYSeries series2 = new XYSeries("Plan 2W");
        final XYSeries series3 = new XYSeries("Plan Total");
        final XYSeries series4 = new XYSeries("Plan Export");
        final XYSeries series5 = new XYSeries("Plan IP");
        final XYSeries series6 = new XYSeries("Plan P/F");
        final XYSeries series7 = new XYSeries("Act 2W");
        final XYSeries series8 = new XYSeries("Act 4W");
        final XYSeries series9 = new XYSeries("Act IP");
        final XYSeries series10 = new XYSeries("Act Total");
        final XYSeries series11 = new XYSeries("Forc");
        
        List<Odc> data = this.dao.getAllByDate(MainController.startDate, MainController.endDate);
        

        
        
            
           
        double planTotal ;
        double actTotal;
               
        for (Odc object : data) {
            String[] date = object.getDate().toString().split("-");
            planTotal = object.getPlan2w()+object.getPlan4w()+object.getPlanexport()+object.getPlanip()+object.getPlanpf();
            actTotal = object.getAct2w()+object.getAct4w()+object.getActexport()+object.getActip()+object.getActpf();

            series1.add(Double.parseDouble(date[2]), object.getPlan4w());
            series2.add(Double.parseDouble(date[2]), object.getPlan2w());
            series3.add(Double.parseDouble(date[2]), planTotal);
            series4.add(Double.parseDouble(date[2]), object.getPlanexport());
            series5.add(Double.parseDouble(date[2]), object.getPlanip());
            series6.add(Double.parseDouble(date[2]), object.getPlanpf());
            series7.add(Double.parseDouble(date[2]), object.getAct2w());
            series8.add(Double.parseDouble(date[2]), object.getAct4w());
            series9.add(Double.parseDouble(date[2]), object.getActip());
            series10.add(Double.parseDouble(date[2]), actTotal);
            
           
            
            
        }


       

        return dataset;
    }

    private DefaultCategoryDataset forcDateSet(){
        DefaultCategoryDataset data1 = new DefaultCategoryDataset();

        String tempDate[] = MainController.startDate.split("-");
            int avgStartMonth = Integer.parseInt(tempDate[1]) + 1;
            if(avgStartMonth > 12){
                avgStartMonth = avgStartMonth - 12;
            }
            int avgEndMonth = Integer.parseInt(tempDate[1]) + 4;
            if(avgEndMonth > 12 ){
                avgEndMonth = avgEndMonth - 12;
            }
            String avgStartDate = tempDate[0]+"-"+avgStartMonth+"-"+tempDate[2];
            String avgEndDate = tempDate[0]+"-"+avgEndMonth+"-"+tempDate[2];
        
        List<Avgodc> data2 = this.avgOdcDao.getAllByDate(avgStartDate,avgEndDate );

        for(Avgodc object : data2){
            String tempDate2[] = object.getDate().toString().split("-");
            String avgMonthName= CommonUtil.getShortMonthName(Integer.parseInt(tempDate2[1])-1);
            data1.addValue(object.getAvgforce(), "Forc", "Forc "+avgMonthName);
            

        }

        return data1;

    }
*/

}

